package com.teacold.customdemo.chapter_two;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.TextView;

import com.teacold.customdemo.R;
import com.teacold.utils.Logger;

import androidx.appcompat.app.AppCompatActivity;

import static android.view.animation.ScaleAnimation.*;

public class Animation_Scale extends AppCompatActivity implements View.OnClickListener {
    private TextView scale_text;
    private Button scale_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_scale_layout);
        initView();
        initMonitor();
    }

    private void initView() {
        scale_text = findViewById(R.id.scale_text);
        scale_button = findViewById(R.id.scale_button);


    }

    private void initMonitor() {
        scale_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scale_button:
                if(scale_text.getVisibility()==View.VISIBLE){
                    Animation animation = getAlphaAnimation();
                    animation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            scale_text.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    scale_text.startAnimation(animation);
                }else {
                    Animation animation = getAlphaAnimation();
                    animation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            scale_text.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            Logger.e("ssssss","vvvvvv执行完");
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    animation.setInterpolator(new AccelerateDecelerateInterpolator());
//                    animation.setInterpolator(new AnticipateInterpolator());
                    scale_text.startAnimation(animation);
                }
//                Animation animation=AnimationUtils.loadAnimation(this,R.anim.scaleanim);
//                Animation animation=AnimationUtils.loadAnimation(this,R.anim.setanim);

                break;
        }
    }

    private Animation getScaleAnimation() {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(700);
        scaleAnimation.setFillAfter(true);
        return scaleAnimation;
    }

    private Animation getAlphaAnimation() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(700);
        alphaAnimation.setFillBefore(false);
//        alphaAnimation.setFillAfter(true);
        return alphaAnimation;
    }


}
